using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Collections;

using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;
using Camalon.Drawing;
using System.ComponentModel.Design.Serialization;
using System.CodeDom;

using System.Reflection;

namespace Camalon.Drawing.Design
{
	/// <summary>
	/// Summary description for ShapeDependencyDesigner.
	/// </summary>
	public class ShapeDependencyDesigner:ComponentDesigner
	{
		public ShapeDependencyDesigner()
		{
			//
			// TODO: Add constructor logic here
			//
		}


		protected virtual string ShapePropertyName
		{
			get{ return string.Empty;}
		}

		IToolboxService _toolboxService;
		protected IToolboxService ToolboxService
		{
			get
			{
				if( _toolboxService == null) _toolboxService = GetService(typeof(IToolboxService)) as IToolboxService;
				return _toolboxService;
			}

		}
		

		IReferenceService _referenceService;
		protected IReferenceService ReferenceService
		{
			get
			{
				if( _referenceService == null) _referenceService = GetService(typeof(IReferenceService)) as IReferenceService;
				return _referenceService;
			}

		}


		IDesignerHost _designerHost;
		protected IDesignerHost DesignerHost
		{
			get
			{
				if( _designerHost == null) _designerHost = GetService(typeof(IDesignerHost)) as IDesignerHost;
				return _designerHost;
			}
		}

		IMenuCommandService _menuCommandService;
		protected IMenuCommandService MenuCommandService
		{
			get
			{
				if( _menuCommandService == null) _menuCommandService = GetService(typeof(IMenuCommandService)) as IMenuCommandService;
				return _menuCommandService;
			}

		}

		ISelectionService _selectionService;
		protected ISelectionService SelectionService
		{
			get
			{
				if( _selectionService == null) _selectionService = GetService(typeof(ISelectionService)) as ISelectionService;
				return _selectionService;
			}
		}

		IComponentChangeService _changeService;
		protected IComponentChangeService ChangeService
		{
			get
			{
				if( _changeService == null) _changeService = GetService(typeof(IComponentChangeService)) as IComponentChangeService;
				return _changeService;
			}
		}

		IComponent _rootComponent;
		protected IComponent RootComponent
		{
			get
			{
			
				if( _rootComponent == null && this.DesignerHost != null)
					_rootComponent = this.DesignerHost.RootComponent;

				return _rootComponent;

			}
		}

		
		protected static PropertyDescriptorCollection ShapeProperties = TypeDescriptor.GetProperties(typeof(Shape));

        public override void InitializeNewComponent(IDictionary defaultValues)
        {
            base.InitializeNewComponent(defaultValues);

            ContainerControl root = this.RootComponent as ContainerControl;
            if (root != null)
            {

                string propName = this.ShapePropertyName;
                if (propName == null || propName.Trim().Length == 0) return;

                Point controlPosition = Control.MousePosition;
                controlPosition = root.PointToClient(controlPosition);

                ShapeControl c = root.GetChildAtPoint(controlPosition) as ShapeControl;

                if (c.Site == null) return;

                if (c == null || c.Shape == null) return;

                PropertyDescriptor prop = ShapeProperties[propName];

                if (prop == null) return;

                prop.SetValue(c.Shape, this.Component);
            }

        }


	}
}
